package com.example.ywr.controller;

import com.example.ywr.common.PageParam;
import com.example.ywr.common.PageResult;
import com.example.ywr.common.Result;
import com.example.ywr.dto.ProductRequest;
import com.example.ywr.entity.Product;
import com.example.ywr.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("添加商品")
    @PostMapping
    public Result<Product> add(@Validated @RequestBody ProductRequest request) {
        Product product = new Product();
        BeanUtils.copyProperties(request, product);
        Product addedProduct = productService.add(product);
        return Result.success(addedProduct);
    }

    @ApiOperation("更新商品")
    @PutMapping("/{id}")
    public Result<Product> update(@PathVariable Long id, @Validated @RequestBody ProductRequest request) {
        Product product = new Product();
        BeanUtils.copyProperties(request, product);
        product.setId(id);
        Product updatedProduct = productService.update(product);
        if (updatedProduct == null) {
            return Result.failed("商品不存在");
        }
        return Result.success(updatedProduct);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = productService.delete(id);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("删除失败");
    }

    @ApiOperation("批量删除商品")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = productService.batchDelete(ids);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量删除失败");
    }

    @ApiOperation("获取商品详情")
    @GetMapping("/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        Product product = productService.getById(id);
        if (product == null) {
            return Result.failed("商品不存在");
        }
        return Result.success(product);
    }

    @ApiOperation("分页查询商品列表")
    @GetMapping
    public Result<PageResult<Product>> list(PageParam pageParam) {
        PageResult<Product> pageResult = productService.list(pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据商品名称模糊查询")
    @GetMapping("/search")
    public Result<PageResult<Product>> listByName(@RequestParam String name, PageParam pageParam) {
        PageResult<Product> pageResult = productService.listByName(name, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据分类ID查询商品")
    @GetMapping("/category/{categoryId}")
    public Result<PageResult<Product>> listByCategoryId(@PathVariable Long categoryId, PageParam pageParam) {
        PageResult<Product> pageResult = productService.listByCategoryId(categoryId, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据状态查询商品")
    @GetMapping("/status/{status}")
    public Result<PageResult<Product>> listByStatus(@PathVariable Integer status, PageParam pageParam) {
        PageResult<Product> pageResult = productService.listByStatus(status, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("更新商品状态")
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean success = productService.updateStatus(id, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("更新状态失败");
    }

    @ApiOperation("批量更新商品状态")
    @PutMapping("/batch/status/{status}")
    public Result<Boolean> batchUpdateStatus(@RequestBody List<Long> ids, @PathVariable Integer status) {
        boolean success = productService.batchUpdateStatus(ids, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量更新状态失败");
    }
}